/**
 * Created by zjx on 2017/1/16.
 */
$(function jsValidateForm() {

    var $form1 = $('#product_single');
    var $edit_form = $('#editor_form');

    var $form2 = $('#color-img');

    $(".control-group.success input").css("color", "#333");
    $(".control-group.success .control-label").css("color", "#333");



    $form1.validate({
        submitHandler: function (form1) {
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));
            } else {
                console.log('Please use after App.js initialized')
            }

            form1.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {
            code: {
                required: true
            },
            glasses_type: {
                required: true
            },
            function_type_id: {
                required:
                    function () {
                        return parseInt($form1.find('.glasses_type').val()) === 2;
                    }
            },
            'func_ids[]': {
                required: true
            },
            frame_id: {
                required: true
            },
            'color_ids[]':{
                required: true
            },
            // eye_size: {
            //     required: true
            // },
            // bridge_size: {
            //     required: true
            // },
            // temple_size: {
            //     required: true
            // },
            brand_id: {
                required: true
            },
            gender: {
                required: true
            },
            type_id: {
                required: true
            },
            mat_id: {
                required: true
            },
            stock: {
                required: true,
                digits:true
            },
            stock_detail: {
                required: true
            },
            sale_at: {
                required: true
            },
            first_title: {
                required: true
            },
            second_title: {
                required: true
            },
            promotion_sale_price: {
                required: true,
                number:true
            },
            sale_price: {
                required: true,
                number:true
            },
            rd_upper_limit: {
                required:
                    function () {
                        return !! $form1.find('.rd').attr('checked');
                    }
            },
            super_ratio : {
                required: true
            },
            ancestor_ratio : {
                required: true
            },
            thumb_img: {
                required: true
            },
            rd_lower_limit :{
                required:
                    function () {
                        return !! $form1.find('.rd').attr('checked');
                    }
            },
            "lens_ids[]":{
                required:
                    function () {
                        return !! $form1.find('.has_lens').attr('checked');
                    }
            }
        },
        messages:{
            code: {
                required: '必填字段'
            },
            glasses_type: {
                required: '必选'
            },
            fuction_type_id: {
                required: '必选'
            },
            'func_ids[]': {
                required: '必选'
            },
            frame_id: {
                required: '必选'
            },
            'color_ids[]': {
                required: '必选'
            },
            eye_size: {
                required: '必选'
            },
            bridge_size: {
                required: '必选'
            },
            temple_size: {
                required: '必选'
            },
            brand_id: {
                required: '必选'
            },
            gender: {
                required: '必选'
            },
            type_id: {
                required: '必选'
            },
            mat_id: {
                required: '必选'
            },
            // has_rd: {
            //     required: ''
            // },
            stock: {
                required: '必填字段',
                digits:'必须为数量'
            },
            stock_detail: {
                required: '必填字段'
            },
            sale_at: {
                required: '必选'
            },

            first_title: {
                required: '必填字段'
            },
            second_title: {
                required: '必填字段'
            },
            promotion_sale_price: {
                required: '必填字段',
                number:'必须为金额'
            },
            sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            rd_lower_limit :{
                required: '必填字段'
            },
            rd_upper_limit :{
                required: '必填字段'
            },
            super_ratio : {
                required: '必填字段'
            },
            ancestor_ratio : {
                required: '必填字段'
            },
            thumb_img: {
                required: '请选择图片'
            },
            "lens_ids[]":{
                required:
                    function () {
                        return !! $edit_form.find('.has_lens').attr('checked');
                    }
            }
        },


        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },


    });

    $form2.validate({
        submitHandler: function (form2) {
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));
            } else {
                console.log('Please use after App.js initialized')
            }

            form2.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {

            product_id: {
                required: true
            },

            color_id: {
                required: true
            },

            color_img :{
                required:
                    function () {
                        return $('#color_img').attr('src') == '';
                    }
            },

        },
        messages:{

            product_id: {
                required: '请选择商品'
            },

            color_id: {
                required: '请选择颜色'
            },

            color_img: {
                required: '请选择图片'
            },

        },


        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        },


    });


    $edit_form.validate({
        submitHandler: function (form1) {
            if (App) {
                App.blockUI($('.portlet'));
                if ($('.modal')) App.blockUI($('.modal'));

            } else {
                console.log('Please use after App.js initialized')
            }

            form1.submit();
        },
        errorElement: 'span', //default input error message container
        errorClass: 'help-inline', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: ".doon-ignore",
        rules: {
            code: {
                required: true
            },
            glasses_type: {
                required: true
            },
            function_type_id: {
                required:
                    function () {
                        return !! $form1.find('.glasses_type').attr(value=='2');
                    }
            },
            'func_ids[]': {
                required: true
            },
            frame_id: {
                required: true
            },
            brand_id: {
                required: true
            },
            gender: {
                required: true
            },
            type_id: {
                required: true
            },
            mat_id: {
                required: true
            },
            stock: {
                required: true,
                digits:true
            },
            stock_detail: {
                required: true
            },
            sale_at: {
                required: true
            },
            first_title: {
                required: true
            },
            second_title: {
                required: true
            },
            promotion_sale_price: {
                required: true,
                number:true
            },
            sale_price: {
                required: true,
                number:true
            },
            rd_upper_limit: {
                required:
                    function () {
                        return !! $form1.find('.rd').attr('checked');
                    }
            },
            super_ratio : {
                required: true
            },
            ancestor_ratio : {
                required: true
            },
            rd_lower_limit :{
                required:
                    function () {
                        return !! $form1.find('.rd').attr('checked');
                    }
            },
            "lens_ids[]":{
                required:
                    function () {
                        return !! $form1.find('.has_lens').attr('checked');
                    }
            }
        },
        messages:{
            code: {
                required: '必填字段'
            },
            glasses_type: {
                required: '必选'
            },
            fuction_type_id: {
                required: '必选'
            },
            'func_ids[]': {
                required: '必选'
            },
            frame_id: {
                required: '必选'
            },
            'color_ids[]': {
                required: '必选'
            },
            brand_id: {
                required: '必选'
            },
            gender: {
                required: '必选'
            },
            type_id: {
                required: '必选'
            },
            mat_id: {
                required: '必选'
            },
            // has_rd: {
            //     required: ''
            // },
            stock: {
                required: '必填字段',
                digits:'必须为数量'
            },
            stock_detail: {
                required: '必填字段'
            },
            sale_at: {
                required: '必选'
            },

            first_title: {
                required: '必填字段'
            },
            second_title: {
                required: '必填字段'
            },
            promotion_sale_price: {
                required: '必填字段',
                number:'必须为金额'
            },
            sale_price: {
                required: '必填字段',
                number: '必须为金额'
            },
            rd_lower_limit :{
                required: '必填字段'
            },
            rd_upper_limit :{
                required: '必填字段'
            },
            super_ratio : {
                required: '必填字段'
            },
            ancestor_ratio : {
                required: '必填字段'
            },
            "lens_ids[]":{
                required:
                    function () {
                        return !! $edit_form.find('.has_lens').attr('checked');
                    }
            }
        },


        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.help-inline').removeClass('ok'); // display OK icon
            $(element)
                .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change dony by hightlight
            $(element)
                .closest('.control-group').removeClass('error'); // set error class to the control group
        },

        success: function (label) {
            label
                .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group

        }

    });

});
